<template>
	<view class="main">
		<nav-bar class="line" isItem>
			<image slot="left" @click="back" src="@/static/img/common/left-arrow.svg" alt="" />

			<view slot="center">确认订单</view>
		</nav-bar>
		<view @click="chooseAddress">
			<view class="address" >
				<view class="view"  v-if="address">
					<view class="view">
						<view class="view">{{ address.receiver}}</view>
						<view class="view">{{address.phone }}</view>
					</view>
					<view>
						{{ fullAddress }}
					</view>
				</view>
				<view style="color: red;" v-else>
					点击此处添加收货地址
				</view>
				<view>
					<image src="../../../static/img/common/more.svg" alt="" style="width: 64rpx; height: 64rpx;" />
				</view>
			</view>
		</view>
		<view class="goods">
			<order-good :goods="goods" />
		</view>

		<view class="payLine" @click="pay">
			<view class="view">在线支付</view>
		</view>
		<pay-order v-if="paying" :price="price" :count="count" :goods="goods" :address_i="address_i" :tag="tag"
			@close="paying = false" />
	</view>
</template>


<script>
	import NavBar from "components/common/NavBar/NavBar";
	import OrderGood from "components/common/Order/OrderGood";
	import payOrder from "./payOrder";

	import {
		mapGetters,
		mapState,
		mapMutations
	} from "vuex";

	import {
		getAddress
	} from "network/user";

	export default {
		components: {
			NavBar,
			payOrder,
			OrderGood,
		},
		data() {
			return {
				buyNow: null,
				paying: false,
				currentAddress: null,
				tag: -1,
			};
		},

		computed: {
			...mapGetters(["getSelectedGood", "reCount"]),
			...mapState(["totalPrice", "address", "buyNowGood", "hasAddress"]),
			fullAddress() {
				return this.address.province + this.address.city + (this.address.area ? this.address.area:'') + this.address.address;
			},

			good() {
				if (this.goods) {
					return Object.values(Object.values(this.goods)[0].goods)[0];
				} else {
					return {};
				}
			},

			price() {
				return this.buyNow ?
					(this.good.price * this.count).toFixed(2) :
					this.totalPrice.toFixed(2);
			},
			count() {
				return this.buyNow ? this.good.count : this.reCount;
			},
			goods() {
				if (this.tag == 0) {
					if (this.getSelectedGood == {}) {
						this.error("获取用户订单信息失败");
						// this.$router.push("/");
						uni.navigateTo({
							url: "/pages/home/Home"
						})
					} else {
						return this.getSelectedGood;
					}
				}
				if (this.tag == 1) {
					return this.buyNowGood;
				}
			},
			address_i() {
				return this.address?.i;
			},
		},
		methods: {
			...mapMutations([, "setAddress"]),

			formateTitle(title) {
				return title.slice(0, 20) + "...";
			},

			pay() {
				console.log('address',this.address)
				if(this.address)
				this.paying = true;
				else{
					this.info('请先添加收货地址',2000);
				}
			},
			chooseAddress() {
				// $router.push('/my/address?pay=1')
				uni.navigateTo({
					url: "/pages/my/address/Address?pay=1",
				})
			}
		},
		onLoad({
			tag
		}) {
			this.tag = tag;
			this.buyNow = tag == 1;
			if (tag != 0 && tag != 1) {
				// this.$router.push("/");
				uni.switchTab({
					url: "/pages/home/Home"
				})
			}

			if (!this.hasAddress) {
				getAddress().then((res) => {
					if (res[1].data.tag) {
						this.currentAddress = res[1].data.data[0];
						this.setAddress(res[1].data.data[0]);
					} else {
						// this.$router.push("/my/address?pay=2");
						uni.navigateTo({
							url: "/pages/my/address/Address?tag=2"
						})
					}
				});
			}
		},
		activated() {
			if (!Object.keys(this.address).length) {
				// this.$router.push("/my/address?pay=2");
				uni.navigateTo({
					url: "/pages/my/address/Address?tag=2"
				})
			}
		},

	};
</script>
<style lang="scss" scoped>
	.main {
		width: 100vw;
		background-color: rgb(242, 242, 242);
	}

	.line {
		border-bottom: 2rpx solid rgb(242, 242, 242);
	}

	.line image {
		width: 64rpx;
		height: 64rpx;
	}

	.address {
		width: 100vw;
		height: 160rpx;
		display: flex;
		flex-direction: row;
		padding: 0 20rpx;
		justify-content: space-between;
		align-items: center;
		background-color: white;
		box-sizing: border-box;
		border-bottom: 6rpx solid #2ed;
	}

	.address > .view:first-child {
		width: calc(100vw - 200rpx);
	}

	.address > .view:first-child > .view:first-child {
		display: flex;
		flex-direction: row;
		/* #ifdef H5 */
		gap: 20rpx;
		/* #endif */
	
		font-size: 36rpx;
		font-weight: 600;
	}
	/* #ifdef MP-WEIXIN || APP */
	.address > .view:first-child > .view:first-child > .view:first-child{
		margin-right: 10rpx;
	}
	/* #endif */

	.payLine {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 140rpx;
		width: 100vw;
		padding: 0 10vw;
		background-color: white;
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		margin-top: 140rpx;
	}

	.payLine > .view {
		width: 80vw;
		height: 90rpx;
		line-height: 90rpx;
		background-color: rgb(55, 131, 253);
		text-align: center;
		border-radius: 40rpx;
		color: white;
	}

	.goods {
		padding-bottom: 140rpx;
	}
</style>
